<?php

include dirname(dirname(dirname(__FILE__))) . '/include/common.php';

class webServiceGetToDoByDay extends core {

    public function process() {
        $limit = $this->limit;
        $limit = 2;
        $page = intval($_POST['page']);
        if ($page < 1) {
            $page = 1;
        }
        $from = ($page - 1) * $limit + 2;
        $user_id = intval($_POST['user_id']);
        $str_day = $_POST['str_day'];
        $html = '';
        $total=0;
        if ($str_day == "past") {
            //today

            $total_past = $this->db->get_row("SELECT count(todo_id) as total FROM todo_lists WHERE user_id={$user_id} AND DATE(start_date) < CURDATE() ;");
            $total = $total_past->total;

            if ($total > 0) {
                $data_past = $this->db->get_results("SELECT * FROM todo_lists WHERE user_id={$user_id} AND DATE(start_date)< CURDATE() ORDER BY start_time asc LIMIT $from,$limit;");
                foreach ($data_past as $todo) {
                    $todo_start_time = strtotime($todo->start_time);
                    $start_date = date('D m/d/Y', strtotime($todo->start_date));
                    if ($todo->end_time == $todo->start_date) {
                        $time = date('h:i A', $todo_start_time);
                    } else {
                        $time = date('h:i A', $todo_start_time) . ' - ' . date('h:i A', strtotime($todo->end_time));
                    }
                   
                    $checked='';
                    if($todo->is_done==1){
                        $checked='checked=checked';
                    }
                    $html.='<li class="wd-list-item-guest wd-list-item-todo past">
                                <div class="wd-list-todo-check">
                                    <input type="checkbox" '.$checked.' id="wd-checkbox-'.$todo->todo_id.'" rel="'.$todo->todo_id.'" name="" class="regular-checkbox-addguest wd-checkbox">
                                    <label for="wd-checkbox-'.$todo->todo_id.'" class="label-checkbox"></label>
                                </div>
                                <div class="wd-list-guest-action">
                                    
                                    <span style="float: right; margin-right: 4px;" class="wd-delete-guest" rel="' . $todo->todo_id . '"><img src="img/delete-icon.png" /></span>
                                </div>
                                <div class="wd-list-guest-content">
                                    <div class="wd-name-guest">' . $todo->todo_item . '</div>
                                    <div class="wd-contact">' . $start_date . '   |   ' . $time . '</div>
                                    
                                    
                                </div>
                            </li>';
                }
            }
        } elseif ($str_day == "today") {
            //today

            $total_today = $this->db->get_row("SELECT count(todo_id) as total FROM todo_lists WHERE user_id={$user_id} AND DATE(start_date)=CURDATE() ;");
            $total = $total_today->total;

            if ($total > 0) {
                $data_today = $this->db->get_results("SELECT * FROM todo_lists WHERE user_id={$user_id} AND DATE(start_date)=CURDATE() ORDER BY start_time asc LIMIT $from,$limit;");
                foreach ($data_today as $todo) {
                    $todo_start_time = strtotime($todo->start_time);
                    $start_date = date('D m/d/Y', strtotime($todo->start_date));
                    if ($todo->end_time == $todo->start_date) {
                        $time = date('h:i A', $todo_start_time);
                    } else {
                        $time = date('h:i A', $todo_start_time) . ' - ' . date('h:i A', strtotime($todo->end_time));
                    }
                    
                    $checked='';
                    if($todo->is_done==1){
                        $checked='checked=checked';
                    }
                    $html.='<li class="wd-list-item-guest wd-list-item-todo today">
                                <div class="wd-list-todo-check">
                                    <input type="checkbox" '.$checked.' id="wd-checkbox-'.$todo->todo_id.'" rel="'.$todo->todo_id.'" name="" class="regular-checkbox-addguest wd-checkbox">
                                    <label for="wd-checkbox-'.$todo->todo_id.'"></label>
                                </div>
                                <div class="wd-list-guest-action">
                                    <a href="editToDo.html" class="wd-edit-guest" rel="' . $todo->todo_id . '"><img src="img/edit-icon.png" /></a>
                                    <span class="wd-delete-guest" rel="' . $todo->todo_id . '"><img src="img/delete-icon.png" /></span>
                                </div>
                                <div class="wd-list-guest-content">
                                    <div class="wd-name-guest">' . $todo->todo_item . '</div>
                                    <div class="wd-contact">' . $start_date . '   |   ' . $time . '</div>
                                    
                                    
                                </div>
                            </li>';
                }
            }
        } elseif ($str_day == "tomorrow") {
            //tomorrow

            $total_tomorrow = $this->db->get_row("SELECT count(todo_id) as total FROM todo_lists WHERE user_id={$user_id} AND DATE(start_date) >= DATE_ADD(CURDATE(),INTERVAL 1 DAY) AND DATE(start_date) < DATE_ADD(CURDATE(),INTERVAL 2 DAY) ;");
            $total = $total_tomorrow->total;
            if ($total > 0) {
                $data_tomorrow = $this->db->get_results("SELECT * FROM todo_lists WHERE user_id={$user_id} AND DATE(start_date) >= DATE_ADD(CURDATE(),INTERVAL 1 DAY) AND DATE(start_date) < DATE_ADD(CURDATE(),INTERVAL 2 DAY) ORDER BY start_time asc LIMIT $from,$limit;");
                foreach ($data_tomorrow as $todo) {

                    $todo_start_time = strtotime($todo->start_time);
                    $start_date = date('D m/d/Y', strtotime($todo->start_date));
                    if ($todo->end_time == $todo->start_date) {
                        $time = date('h:i A', $todo_start_time);
                    } else {
                        $time = date('h:i A', $todo_start_time) . ' - ' . date('h:i A', strtotime($todo->end_time));
                    }
                    
                    $html.='<li class="wd-list-item-guest wd-list-item-todo tomorrow">
                                <div class="wd-list-todo-check">
                                    <input type="checkbox" '.$checked.' id="wd-checkbox-'.$todo->todo_id.'" rel="'.$todo->todo_id.'" name="" class="regular-checkbox-addguest wd-checkbox">
                                    <label for="wd-checkbox-'.$todo->todo_id.'"></label>
                                </div>
                                <div class="wd-list-guest-action">
                                    <a href="editToDo.html" class="wd-edit-guest" rel="' . $todo->todo_id . '"><img src="img/edit-icon.png" /></a>
                                    <span class="wd-delete-guest" rel="' . $todo->todo_id . '"><img src="img/delete-icon.png" /></span>
                                </div>
                                <div class="wd-list-guest-content">
                                    <div class="wd-name-guest">' . $todo->todo_item . '</div>
                                    <div class="wd-contact">' . $start_date . '   |   ' . $time . '</div>
                                    
                                    
                                </div>
                            </li>';
                }
            }
        } elseif ($str_day == "later") {
            //later
           
            $total_later = $this->db->get_row("SELECT count(todo_id) as total FROM todo_lists WHERE user_id={$user_id} AND DATE(start_date) >= DATE_ADD(CURDATE(),INTERVAL 2 DAY) ;");
            $total= $total_later->total;
            if ($total > 0) {
                $data_later = $this->db->get_results("SELECT * FROM todo_lists WHERE user_id={$user_id} AND DATE(start_date) >= DATE_ADD(CURDATE(),INTERVAL 2 DAY) ORDER BY start_time asc LIMIT $from,$limit;");
                foreach ($data_later as $todo) {

                    $todo_start_time = strtotime($todo->start_time);
                    $start_date = date('D m/d/Y', strtotime($todo->start_date));
                    if ($todo->end_time == $todo->start_date) {
                        $time = date('h:i A', $todo_start_time);
                    } else {
                        $time = date('h:i A', $todo_start_time) . ' - ' . date('h:i A', strtotime($todo->end_time));
                    }
                    
                    $html.='<li class="wd-list-item-guest wd-list-item-todo later">
                                <div class="wd-list-todo-check">
                                    <input type="checkbox" '.$checked.' id="wd-checkbox-'.$todo->todo_id.'" rel="'.$todo->todo_id.'" name="" class="regular-checkbox-addguest wd-checkbox">
                                    <label for="wd-checkbox-'.$todo->todo_id.'"></label>
                                </div>
                                <div class="wd-list-guest-action">
                                    <a href="editToDo.html" class="wd-edit-guest" rel="' . $todo->todo_id . '"><img src="img/edit-icon.png" /></a>
                                    <span class="wd-delete-guest" rel="' . $todo->todo_id . '"><img src="img/delete-icon.png" /></span>
                                </div>
                                <div class="wd-list-guest-content">
                                    <div class="wd-name-guest">' . $todo->todo_item . '</div>
                                    <div class="wd-contact">' . $start_date . '   |   ' . $time . '</div>
                                    
                                    
                                </div>
                            </li>';
                }
            }
           
        }
        
            if (ceil(($total-2) / $limit) <= $page) {
                $end_paging = 1;
            } else {
                $end_paging = 0;
            }
        $this->_JSONResponse(array('error' => 0, 'html' => $html, 'page' => $page + 1, 'end_paging' => $end_paging));
    }

}

$processor = new webServiceGetToDoByDay();
$processor->process();
?>